package com.googlecode.tabletags.urlbuilder;

import java.util.Map;

import org.apache.struts2.dispatcher.mapper.ActionMapping;
import org.apache.struts2.portlet.util.PortletUrlHelper;

import com.opensymphony.xwork2.util.ValueStack;

/**
 * Portal UrlBuilder.  Additional parameters needed by this builder:
 * <ul>
 * <li>portletMode - The resulting portlet mode</li>
 * <li>windowState - The resulting portlet window state</li>
 * <li>portletUrlType - Specifies if this should be a portlet render or action url</li>
 * </ul>
 */
public class PortletUrlBuilder implements UrlBuilder {

    public String buildUrl(ValueStack stack, ActionMapping mapping) {
        String portletUrlType = findAttribute(stack, "portletUrlType");
        String portletMode = findAttribute(stack, "portletMode");
        String windowState = findAttribute(stack, "windowState");
        return PortletUrlHelper.buildUrl(mapping.getName(), mapping.getNamespace(), 
            mapping.getParams(), portletUrlType, portletMode, windowState);
    }

    protected String findAttribute(ValueStack stack, String name) {
        return (String) stack.findValue(name, String.class);
    }

    public String buildUrl(ValueStack stack, String uri, Map parameters) {
        return PortletUrlHelper.buildResourceUrl(uri, parameters);
    }
}
